package com.wbpay.sdk.dlg;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wbpay.sdk.framework.IUserFunction;
import com.wbpay.sdk.params.User;
import com.wbpay.sdk.utils.Utils;
import com.wbpay.sdk.widget.ClearTextWatcher;

import java.util.List;

/**
 * 登录dialog
 *
 * @author guangqing_lu
 */
public class LoginDialog extends Dialog implements View.OnClickListener {

    private Button mLoginButton;// 登录按钮
    private Button mRegisterButton;// 注册按钮
    private EditText mAccountEdit;// 用户名输入框
    private EditText mPasswordEdit;// 密码输入框
    private CheckBox mRememberCheck; // 记住密码
    private TextView mForgotText;// 忘记密码
    private ImageButton mClearAccountIC;// 清除帐号
    private ImageButton mClearPasswordIC;// 清除密码
    private ImageButton mAccountChooseButton;
    private LinearLayout mAccountLinear;
    private TextView tv_kf; // 客服按钮

    private IUserFunction mUserFunction;

    public LoginDialog(Context context, IUserFunction l) {
        super(context);
        mUserFunction = l;
        setCanceledOnTouchOutside(false);
        Window window = getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        WindowManager.LayoutParams wAttrs = window.getAttributes();
        wAttrs.gravity = Gravity.CENTER | Gravity.FILL_HORIZONTAL;
        window.setWindowAnimations(android.R.style.Animation_Dialog);
        window.setBackgroundDrawable(getContext().getResources().getDrawable(
                (android.R.color.transparent)));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(Utils.getLayoutId(getContext(), "jy_dialog_login"));
        initView();
        initData();
    }

    private void initView() {
        mForgotText = (TextView) findViewById(Utils.getId(getContext(), "text_forgot"));
        mLoginButton = (Button) findViewById(Utils.getId(getContext(), "btn_login"));
        mRegisterButton = (Button) findViewById(Utils.getId(getContext(), "btn_register"));
        mAccountEdit = (EditText) findViewById(Utils.getId(getContext(), "edit_account"));
        mPasswordEdit = (EditText) findViewById(Utils.getId(getContext(), "edit_password"));
        mRememberCheck = (CheckBox) findViewById(Utils.getId(getContext(), "check_remember"));
        mClearAccountIC = (ImageButton) findViewById(Utils.getId(getContext(), "clear_edit_account"));
        mClearPasswordIC = (ImageButton) findViewById(Utils.getId(getContext(), "clear_edit_password"));
        mAccountChooseButton = (ImageButton) findViewById(Utils.getId(getContext(), "button_account_more"));
        mAccountLinear = (LinearLayout) findViewById(Utils.getId(getContext(), "linear_account"));
        tv_kf = (TextView) findViewById(Utils.getId(getContext(), "tv_kf"));

        mAccountChooseButton.setOnClickListener(this);
        mForgotText.setOnClickListener(this);
        mRegisterButton.setOnClickListener(this);
        mLoginButton.setOnClickListener(this);
        mClearAccountIC.setOnClickListener(this);
        mClearPasswordIC.setOnClickListener(this);
        mAccountEdit.addTextChangedListener(new ClearTextWatcher(mClearAccountIC));
        mPasswordEdit.addTextChangedListener(new ClearTextWatcher(mClearPasswordIC));
        tv_kf.setOnClickListener(this);
    }

    private void initData() {
        updateData();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == Utils.getId(getContext(), "btn_login")) { // 登陆按钮
            if (mUserFunction != null) {
                mUserFunction.doLogin(mAccountEdit.getText().toString().trim(), mPasswordEdit.getText().toString().trim(), mRememberCheck.isChecked());
            }
        } else if (v.getId() == Utils.getId(getContext(), "btn_register")) { // 注册按钮
            if (mUserFunction != null) {
                mUserFunction.callRegister();
            }
        } else if (v.getId() == Utils.getId(getContext(), "text_forgot")) { // 忘记密码
            if (mUserFunction != null) {
                mUserFunction.showForgetPassword();
            }
        } else if (v.getId() == Utils.getId(getContext(), "clear_edit_account")) { // 清除账号
            mAccountEdit.setText("");
            mAccountEdit.requestFocus();
            mAccountEdit.setError(null);
        } else if (v.getId() == Utils.getId(getContext(), "clear_edit_password")) { // 清除密码
            mPasswordEdit.setText("");
            mPasswordEdit.requestFocus();
            mPasswordEdit.setError(null);
        } else if (v.getId() == Utils.getId(getContext(), "button_account_more")) { // 更多
            if (mUserFunction != null) {
                mUserFunction.showAccountPopup(mAccountEdit);
            }
        } else if (v.getId() == Utils.getId(getContext(), "tv_kf")) { // 客服按钮
            if (mUserFunction != null) {
                mUserFunction.showCustomerService();
            }
        }
    }

    /**
     * 设置账户输入框的错误信息
     *
     * @param error
     */
    public void setAccountError(String error) {
        mAccountEdit.setError(error);
        mAccountEdit.requestFocus();
    }

    /**
     * 设置密码输入框的错误信息
     *
     * @param error
     */
    public void setPasswordError(String error) {
        mPasswordEdit.setError(error);
        mPasswordEdit.requestFocus();
    }

    public void setUserNameAndPassword(String username, String password) {
        mAccountEdit.setText(username);
        mPasswordEdit.setText(password);
    }

    /**
     * 通过外部更新选择账号按钮
     */
    private void updateData() {
        List<User> list = mUserFunction.loadUsers();
        User lastUser = null;
        if (list != null && list.size() > 0) {
            lastUser = list.get(list.size() - 1);
        }
        if (lastUser != null && !TextUtils.isEmpty(lastUser.getUsername()) && !TextUtils.isEmpty(lastUser.getPassword())) {
            mAccountEdit.setText(lastUser.getUsername());
            mPasswordEdit.setText(lastUser.getPassword());
            mRememberCheck.setChecked(mUserFunction.isRememberPassword());
        }
        if (list != null && list.size() > 1) {
            mAccountChooseButton.setVisibility(View.VISIBLE);
        } else {
            mAccountChooseButton.setVisibility(View.GONE);
        }
    }

    /**
     * 删除账号后更新界面
     */
    public void updateUserNameAndPassword() {
        updateData();
    }
}
